var thisPage = {
    first: 0,
    // 初始化页面
    OnPageLoad: function () {
        // 初始化树形控件
        $('#treeContent').jstree({
            "core": {
                "animation": 200,
                "multiple": false,
                "check_callback": true,
                "themes": {
                    "theme": "default",
                    "responsive": true,
                    "dots": false,
                    "icons": true
                },
                "data": {
                    'dataType': 'JSON',
                    'contentType': "application/json; charset=utf-8",
                    'type': "GET",
                    'url': parent.basePath + '/master/user_group/tree.html',
                    'data': function (node) {
                        var _nodeId = node.id === '#' ? -1 : (node.id === 'j1_1' ? 0 : node.id);
                        return {'parentKey': _nodeId};
                    },
                    'complete': function () {
                        if (thisPage.first === 0) {
                            // 打开根节点
                            setTimeout(function () {
                                var ref = $.jstree.reference('treeContent');
                                var _obj = $("#j1_1");
                                ref.open_node(_obj);
                                thisPage.first = 1;
                                $('#loading').hide();
                            }, 500);
                        }
                    }
                }
            },
            "types": {
                "#": {"max_depth": -1, "max_children": -1, "valid_children": ["root"]},
                "root": {"max_depth": -1, "max_children": -1, "icon": parent.basePath + "/images/tree/root.png", "valid_children": -1},
                "default": {"max_depth": -1, "max_children": -1, "valid_children": -1},
                "leaf": {"max_depth": -1, "max_children": -1, "icon": parent.basePath + "/images/tree/folder_close.png", "valid_children": -1}
            },
            "plugins": ["types", "wholerow"]
        });
        // 树形节点选择
        $('#treeContent').on('select_node.jstree', function (event, data) {
            var _selectedNodeId = data.node.id;
            var _select_node = $("#" + _selectedNodeId);
            _selectedNodeId = _selectedNodeId === "j1_1" ? 0 : _selectedNodeId;
            // 设置选择结果
            var item = new Array(_selectedNodeId, _select_node.attr("fullName"));
            window.parent.arrChoice.push(item);
            window.parent.choice_success = true;
            closePopupWnd();
        });
        // 节点展开后，重新设置图标
        $('#treeContent').on('open_node.jstree', function (event, data) {
            var ref = $.jstree.reference('treeContent');
            var _nodeIdOfSelected = data.node.id;
            var _nodeOfSelected = $("#" + _nodeIdOfSelected);
            if (_nodeIdOfSelected !== "j1_1")
                ref.set_icon(_nodeOfSelected, parent.basePath + "/images/tree/folder_open.png");
        });
        // 节点收回后，重新设置图标
        $('#treeContent').on('close_node.jstree', function (event, data) {
            var ref = $.jstree.reference('treeContent');
            var _nodeIdOfSelected = data.node.id;
            var _nodeOfSelected = $("#" + _nodeIdOfSelected);
            if (_nodeIdOfSelected !== "j1_1")
                ref.set_icon(_nodeOfSelected, parent.basePath + "/images/tree/folder_close.png");
        });
    }
};

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    thisPage.OnPageLoad();
}

function onPageResize() {
    var h = $(window).height();
    var w = $(window).width();

    $("#treeArea").css({width: w - 16, top: 8, left: 8, height: h - 16});

    over = 1;
}